package com.blackhorse.service.fallback;

import com.blackhorse.domain.Product;
import com.blackhorse.service.ProductService;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 写法2：Feign整合sentinel容错的工厂类，可以获取发生的异常
 * 需要实现FallbackFactory接口，泛型指定feign接口
 */
@Slf4j
@Service
public class ProductServiceFallbackFactory implements FallbackFactory<ProductService> {

    @Override
    public ProductService create(Throwable e) {
        //在这里创建productService容错实现类
        return new ProductService() {
            @Override
            public Product findByPid(Integer pid) {
                log.error("findByPid出错 异常：", e);
                //容错逻辑
                Product product = new Product();
                product.setPid(-100);
                product.setPname("调用商品微服务出现异常，进入容错逻辑");
                return product;
            }

            @Override
            public void reduceInventory(Integer pid, Integer number) {
                log.error("reduceInventory 容错逻辑 pid:{} number:{}", pid, number, e);
            }
        };
    }
}
